package com.ruibang.glass.etl.handler.main;


import com.gitee.Jmysy.binlog4j.core.BinlogEvent;
import com.gitee.Jmysy.binlog4j.core.IBinlogEventHandler;
import com.gitee.Jmysy.binlog4j.springboot.starter.annotation.BinlogSubscriber;
import com.ruibang.glass.etl.entity.MaterialLotItem;
import com.ruibang.glass.etl.mapper.MainMaterialLotItemMapper;
import org.springframework.beans.factory.annotation.Autowired;

@BinlogSubscriber(clientName = "business")
public class MainMaterialLotItemEventHandler implements IBinlogEventHandler<MaterialLotItem> {

    @Autowired
    private MainMaterialLotItemMapper mainMaterialLotItemMapper;

    @Override
    public void onInsert(BinlogEvent<MaterialLotItem> event) {
        if(1 > mainMaterialLotItemMapper.insert(event.getData())){
            System.out.println("g_material_lot_item main插入数据失败:" + event.getData());
        }else
            System.out.println("g_material_lot_item main插入数据成功");
    }

    @Override
    public void onUpdate(BinlogEvent<MaterialLotItem> event)  {
        if(1 > mainMaterialLotItemMapper.updateById(event.getData())){
            System.out.println("g_material_lot_item 更新数据失败:" + event.getData());
            onInsert(event);
        }else
            System.out.println(event.getDatabase() + "-" +
                    event.getTable() + " main更新数据成功");
    }

    @Override
    public void onDelete(BinlogEvent<MaterialLotItem> event) {
        if(1 > mainMaterialLotItemMapper.deleteById(event.getData())){
            System.out.println("g_material_lot_item main删除数据失败:" + event.getData());
        }else
            System.out.println("g_material_lot_item main删除数据成功");
    }

    @Override
    public boolean isHandle(String database, String table) {
        return database.equals("glass") && table.equals("g_material_lot_item");
    }
}
